/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package org.javaongems.client.stdctrls;

import org.javaongems.client.Gem;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * This is a reimplemenation of PNGImage widget developed by Robert Hanson.
 * 
 * <p>Acknowledgements:
 * <ul>
 * 	<li>Robert Hanson whose code taught me how to do user-agent specific implementation
 * 	 using GWT. Thanks mate!
 * 	<li> Dojo toolkit whose impl uses the image tag with style additions
 * </ul>
 * </p>
 * 
 * <p>Differences to Robert's impl:
 * <ul>
 * 	<li> Uses the original img DOM element
 * 	<li> this has attempted to be more true to Liskov Substitution Principle 
 * 		(LSP)
 * </ul>
 * </p>
 * @author Ashin Wimalajeewa (ash) <br/>
 * 
 * Robert's impl: https://svn.sourceforge.net/svnroot/gwt-widget/trunk
 */
public class Image extends com.google.gwt.user.client.ui.Image {
	String url;	// it gets overwritten when playing with the divs
	
	public Image(String url) {
		super(url);
	}
	
	public Image() {
		super();
	}

	public void setUrl(String url) {
		super.setUrl(url);
		this.url = url;
		refresh();
	}
	
	public String getUrl() {
		return url;
	}
	
	public void refresh() {
		prepareDom();
	}
	
	private void prepareDom() {
        Impl impl = (Impl) GWT.create(Impl.class);
        impl.prepare(this);
	}
	
	static public class Impl {	// default impl works on most browsers
		public void prepare(Image src) {
			// do nothing
		}
	}
	
	static public class ImplIe6 extends Impl {
		public void prepare(Image src) {
			String url = src.getUrl();
			if (url == null || !url.toLowerCase().endsWith(".png"))
				return;
			injectStyleFilter(src);
		}
		
		private void injectStyleFilter(Image src) {
			Element srcElem = src.getElement();
            StringBuffer filter = new StringBuffer("progid:DXImageTransform.Microsoft.AlphaImageLoader(src='");
            filter.append(src.getUrl()).append("', sizingMethod='scale')");
            int w = DOM.getIntAttribute(srcElem, "width"), h = DOM.getIntAttribute(srcElem, "height"); 
            String blank = Gem.stone.getBlankImageUri();
            DOM.setAttribute(srcElem, "src", blank);
			DOM.setStyleAttribute(srcElem, "filter", filter.toString());
			DOM.setIntAttribute(srcElem, "width", w);
			DOM.setIntAttribute(srcElem, "height", h);
		}
	}
}